package amdocs.utils.wishlistapp;

import java.net.UnknownHostException;

import com.mongodb.DB;
import com.mongodb.Mongo;
import com.mongodb.WriteConcern;

/**
 * @author AMITJHA
 * Helper class for all db transactions with mongo
 */
public class MongoDBHelper {


	
	static MongoDBHelper instance;
	
	/**
	 * @param args
	 */
	public static void main(String[] args) {

	}
	
	
	private MongoDBHelper(){
		
	}
	
	public static MongoDBHelper getInstance(){
		
		if(instance == null)
			initialize();
		return instance;
	}

	//TODO move to spring
	String serverName ="ranr02v";
	String dbName = "wishlistdb";
	static Mongo connection = null;
	DB db;
	
	public Mongo getConnection() {
		return connection;
	}

	
	public DB getDB(String dbname) {
		return connection.getDB(dbname);
	}

	public DB getDB(){
		
		if(db==null)
			db = connection.getDB(dbName);
		return db;
	}
	
	public void useDB(String dbName){
		db = getDB(dbName);
	}

	//private void setConnection(Mongo connection) {
		//this.connection = connection;
//	}


	private static void initialize() {
		
		instance = new MongoDBHelper();
		try {
			connection = new Mongo("ranr02v");
			WriteConcern wc = new WriteConcern(1,2000);
			connection.setWriteConcern(wc);
			
		} catch (UnknownHostException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		
	}
	

}
